<?php
include_once "baseChannel.php";

class dayuanren extends baseChannel {
    public $userid;
    public $apikey;

    public function __construct($setting){
        parent::__construct($setting);
        $this->userid = $setting['userid'];
        $this->apikey = $setting['apikey'];
    }
    /*通用签名方法*/
    public function getSign($params){
        unset($params['sign']);
        ksort($params);
        $params['apikey'] = $this->apikey;
        $str = http_build_query($params);
        $str = urldecode($str);
//        echo "签名字符串：{$str}\n";
        return strtoupper(md5($str));
    }
    /*查询余额*/
    public function queryBalance($params=[]){
        $url = $this->api_url."/index/user";
        $post_data = [
            'userid'=>$this->userid,
        ];
        $sign = $this->getSign($post_data);
        $post_data['sign'] = $sign;

        $return_str = $this->httpRequest($url,'POST',$post_data);
        $return_data = json_decode($return_str,1);
        if($return_data['errno'] == 0){
            return [
                'error'=>0,
                'msg'=>'查询成功',
                'balance'=> $return_data['data']['balance'] ?? 0, //余额
                'credit'=> 0, //授信
                'code'=>200
            ];
        }
        return [
            'error'=>1,
            'msg'=>$return_data['errmsg'] ?? '查询失败',
            'code'=>500
        ];

    }
    public function queryProduct($params=[]){
        $url = $this->api_url."/index/product";
        $post_data = [
            'userid'=>$this->userid,
//            'type' => '', //产品类型
//            'cate_id' => '', //分类ID
        ];
        $sign = $this->getSign($post_data);
        $post_data['sign'] = $sign;

        $return_str = $this->httpRequest($url,'POST',$post_data);
        $return_data = json_decode($return_str,1);
        $product_map = [];
        if($return_data['errno'] == 0){
            $product_list = $return_data['data'];
            foreach ($product_list as $product){
                $items = $product['products'];
                foreach ($items as $item){
                    $channel_api_code = $item['id'];
                    $channel_product_status = 1;
                    $channel_product_price = $item['price'];

                    $product_map[$channel_api_code] = [
                        'channel_id' => $this->channel_id ?? 0,
                        'channel_api_code' => $channel_api_code,
                        'channel_product_status' => $channel_product_status,
                        'channel_product_price' => $channel_product_price,
                    ];
                }
            }
            return [
                'error'=>0,
                'msg'=>'查询成功',
                'data'=> $product_map ?? [], //余额
                'code'=>200
            ];
        }

        return [
            'error'=>1,
            'msg'=>'查询失败',
            'data'=> [], //余额
            'code'=>500
        ];
    }
    public function queryOrder($params){
        $url = $this->api_url."/index/check";
        $post_data = [
            'userid'=>$this->userid,
            'out_trade_nums' => $params['push_order_sn'], //产品类型
        ];
        $sign = $this->getSign($post_data);
        $post_data['sign'] = $sign;

        $return_str = $this->httpRequest($url,'POST',$post_data);
        $return_data = json_decode($return_str,1);

        $ret = [
            'error'=>0,
            'order_status' => 2,
            'msg'=>'初始化',
            'pz' => '', //凭证
            'post_data'=> json_encode($post_data), //提交字符串
            'channel_data'=> $return_str, //返回字符串
            'code' => 200
        ];

        $ret['msg'] = '充值中';
        if(isset($return_data['errno']) && $return_data['errno'] == 0){
            if($return_data['data'][0]['state'] == 1){
                $ret['order_status'] = self::OrderSuccess;
                $ret['msg'] = '充值完成';
            }
            if($return_data['data'][0]['state'] == 2){
                $ret['order_status'] = self::OrderFail;
                $ret['msg'] = '充值失败';
            }
        }

        return $ret;
    }
    public function submitOrder($mobile, $code, $params=[]){
        $url = $this->api_url."/index/recharge";
        $post_data = [
            'out_trade_num'=>$params['push_order_sn'],
            'product_id'=>$code,
            'mobile'=>$mobile,
            'notify_url'=>$this->callback_url,
            'userid'=>$this->userid,
        ];
        $sign = $this->getSign($post_data);
        $post_data['sign'] = $sign;

        $return_str = $this->httpRequest($url,'POST',$post_data);
        $return_data = json_decode($return_str,1);

        $ret = [
            'error'=> -1,
            'msg'=>'初始化',
            'channel_order' => $return_data['data']['order_number'] ?? '',
            'post_data' => json_encode($post_data),
            'channel_data' => $return_str,
            'code' => 200,
            'channel_order_price' => $return_data['data']['total_price'] ?? 0,
            'channel_balance' => null,
        ];
        if($return_data['errno'] == 0){
            $ret['error'] = 0;
            $ret['msg'] = $return_data['errmsg'] ?? '下单成功';
        }else{
            if (isset($return_data['errno'])){
                $ret['error'] = 1;
                $ret['code'] = 500;
                $ret['msg'] = $return_data['errmsg'] ?? '下单失败';
            }
        }

        return $ret;
    }

    public function callback($params){
        $this->callback_params = $params;
        $this->channel_order_sn = trim($params['order_number']);
        $this->push_order_sn = $params['out_trade_num'];
        //返回参数
        $res = $this->initCallBackParam();
        //订单状态
        $order_status = $params['state'];
        $msg = $params['remark'];
        //凭证
        $pz = $params['voucher'];
        /*
        $sign = $this->checkCallbackSign($params);
        if($sign != $params['sign']){
            $res['error'] = 1;
            $res['msg'] = '签名不匹配';
            $res['resp_data'] = 'fail';
            return $res;
        }
        */
        if($order_status==1){
            $res['order_status'] = self::OrderSuccess;
            $res['msg'] = $msg ?? '充值完成';
        }

        if($order_status==2){
            $res['order_status'] = self::OrderFail;
            $res['msg'] = $msg ?? '充值失败';
        }
        return $res;
    }
}
